/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.qmul.ns2.agent;

import edu.qmul.ns2.computer.*;
import edu.qmul.ns2.DrawElement;
import edu.qmul.ns2.MainFrame;
import java.awt.Component;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JToggleButton;
import javax.swing.SwingUtilities;
import me.zhy.swing.AWTUtilities;

/**
 * 创建DrawAgent,监听 MainPanel 组件
 * @author Moon
 */
public class DrawAgent extends MouseAdapter implements DrawElement {

    /**
     * 业务结束时触发的操作
     */
    private final JToggleButton chooseButton;
    private AgentProperty agentProperty;
    private AgentLabel agent;
    private int at;
    private static int agentID;

    /**
     *
     * @param chooseButton
     * @param a is the type of the selected agent
     */
    public DrawAgent(JToggleButton chooseButton, int a) {
        this.chooseButton = chooseButton;
        this.at = a;
    }

    @Override
    public void mouseMoved(MouseEvent evt) {
        MainFrame frame = (MainFrame) SwingUtilities.getAncestorOfClass(MainFrame.class, evt.getComponent());
        JLabel stateLabel = frame.getStateLabel();
        stateLabel.setText(evt.getX() + "," + evt.getY());
    }

    @Override
    public void mousePressed(MouseEvent evt) {
        Container container = (Container) evt.getComponent();
        switch (at) {
            case AgentType.TCP:
                agent = new TCPLabel();
                agent.setID(this.agentID);
                ++agentID;
                break;
            case AgentType.UDP:
                agent = new UDPLabel();
                agent.setID(this.agentID);
                ++agentID;
                break;
            case AgentType.NULL:
                agent = new AgentLabel();
                agent.setID(this.agentID);
                ++agentID;
                break;
        }
        agent.addMouseListener(new MouseAdapter() {

            @Override
            public void mousePressed(final MouseEvent evt) {
                if (evt.getButton() != MouseEvent.BUTTON3) {
                    agent.requestFocus();
                    return;
                }
                JPopupMenu menu = new JPopupMenu();
                JMenuItem setInfo = new JMenuItem("PROPERTY");
                setInfo.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        final MainFrame frame = (MainFrame) SwingUtilities.getAncestorOfClass(MainFrame.class, evt.getComponent());
                        agentProperty = new AgentProperty(frame, true);
                        agentProperty.setTitle("Agent Property");
                        agentProperty.setVisible(true);

                    }
                });
                JMenuItem delete = new JMenuItem("DELETE");
                delete.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        AWTUtilities.remove(agent);
                    }
                });
                menu.add(setInfo);
                menu.add(delete);
                menu.show(evt.getComponent(), evt.getX(), evt.getY());
            }
        });

        agent.addComponentListener(new ComponentAdapter() {

            @Override
            public void componentMoved(ComponentEvent e) {
                if (infoPanel != null) {
                    Component comp = e.getComponent();
                    infoPanel.setNodeIDText(new Integer(agent.getID()).toString());
                    infoPanel.setX(String.valueOf(comp.getX()));
                    infoPanel.setY(String.valueOf(comp.getY()));
                }
            }
        });

        agent.addFocusListener(new FocusListener() {

            /**
             * 当 Computer 组件获得焦点时触发的操作
             * @param e 焦点事件
             */
            @Override
            public void focusGained(FocusEvent e) {
                MainFrame frame = (MainFrame) SwingUtilities.getAncestorOfClass(MainFrame.class, e.getComponent());
                if (frame == null) {
                    return;
                }
                frame.setInfoComponent(infoPanel = new InfoPanel());
                infoPanel.setNodeIDText(new Integer(agent.getID()).toString());
                infoPanel.setX(String.valueOf(agent.getX()));
                infoPanel.setY(String.valueOf(agent.getY()));
            }

            @Override
            public void focusLost(FocusEvent e) {
                MainFrame frame = (MainFrame) SwingUtilities.getAncestorOfClass(MainFrame.class, e.getComponent());
                if (frame == null) {
                    return;
                }
                infoPanel = null;
                frame.setInfoComponent(null);
            }
        });
        container.add(agent);
        agent.setDraggable(true);
        Dimension mini = agent.getMinimumSize();
        int width = mini.width;
        int height = mini.height;
        agent.setBounds(evt.getX() - width / 2, evt.getY() - height / 2, width, height);
        chooseButton.setSelected(true);
    }

    @Override
    public void start(Component comp) {
        AWTUtilities.removeChildrenNotify((Container) comp);
        comp.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    }

    @Override
    public void stop(Component comp) {
        AWTUtilities.addChildrenNotify((Container) comp);
        comp.setCursor(Cursor.getDefaultCursor());
    }
    private InfoPanel infoPanel;
}
